/*
 * @Author: 107w 8801453+lu-shangru@user.noreply.gitee.com
 * @Date: 2023-10-28 17:45:21
 * @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @LastEditTime: 2024-04-15 17:43:22
 * @Description:
 *
 * Copyright (c) 2023 by 107w, All Rights Reserved.
 */
export const buttonPermission = {
  data() {
    return {
      buttonPerminssion: []
    }
  },
  methods: {
    // 根据路径找到对应菜单权限
    findMenuBylinkPath(linkPath) {
      const menu = this.$store.state.user.menu
      let currMenuNode = null
      function traverse(nodes) {
        if (!nodes) return
        for (var i = 0; i < nodes.length; i++) {
          var node = nodes[i]
          // 按钮选项或者节点是自己的都禁用选择
          if (node.linkPath === linkPath) {
            currMenuNode = node
            return
          }
          if (node.childList) {
            traverse(node.childList)
          }
        }
      }
      traverse(menu)
      return currMenuNode
    },
    // 按钮权限 传入该目录linkPath,返回有权限的按钮数组['assets_edit']
    getButtonPerminssion() {
      const linkPath = this.$route.name
      if (!linkPath) return
      const currMenuNode = this.findMenuBylinkPath(linkPath) || {}
      currMenuNode.childList = currMenuNode?.childList ?? []
      this.buttonPerminssion = currMenuNode?.childList.map(item => item.linkPath)
    },
    // 判断是否具有终端权限
    iSHaveDesignatePerminssion(linkPath) {
      const currMenuNode = this.findMenuBylinkPath(linkPath)
      return currMenuNode !== null
    },
    // 判断操作列是否需要显示
    isShowOperationCol(operationButtons) {
      return operationButtons.some(item => this.buttonPerminssion.includes(item))
    }
  },
  created() {
    this.getButtonPerminssion()
  }
}
